package org.example.smartEducationBackend.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.example.smartEducationBackend.entity.POJO.UserVideoWatch;
import org.example.smartEducationBackend.mapper.UserVideoWatchMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserVideoWatchService {
    @Autowired
    private UserVideoWatchMapper userVideoWatchMapper;
    public Integer update(UserVideoWatch userVideoWatch) {
        userVideoWatch.setWatchTime(new Date());
        QueryWrapper<UserVideoWatch> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userVideoWatch.getUserId());
        wrapper.eq("video_id", userVideoWatch.getVideoId());
        UserVideoWatch userVideoWatch1 = userVideoWatchMapper.selectOne(wrapper);
        System.out.println(userVideoWatch);


        if(userVideoWatch1 != null){
            Float before = userVideoWatch1.getWatchDuration();
            Float now = userVideoWatch.getWatchDuration();
            userVideoWatch.setWatchDuration(before+now);
            userVideoWatch.setWatchId(userVideoWatch1.getWatchId());
            userVideoWatch.setWatchNumber(userVideoWatch1.getWatchNumber()+1);
            UpdateWrapper<UserVideoWatch> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("watch_id",userVideoWatch.getWatchId());
            int i = userVideoWatchMapper.update(userVideoWatch,updateWrapper);
            return i;


        }else {
            userVideoWatch.setWatchNumber(1);
            int insert = userVideoWatchMapper.insert(userVideoWatch);
            return insert;

        }



    }
}
